/**
 * Created by chenwenhao on 2017/1/13.
 * @description 此模块是所有http请求的方法跟模块
 * @param APP_HPST = 'http://m.htxk.com.dev/api/v1'; 开发环境IP ADDRESS
 * @param APP_HOST = 'http://m.htxk.com/api/v1'; 测试环境IP ADDRESS
 * @param APP_HOST = 'http://m.oooseed.com/api/v1'; 生产环境IP ADDRESS
 */
import {Injectable} from '@angular/core';
import {Http, Response, Headers, ResponseOptions, RequestOptions, RequestOptionsArgs} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import {environment} from "@environments/environment";
import {ToolsService} from "@shared/service/tools/tools.service";
import {API_VERSION, CLIENT_TYPE} from "@core/globals/globals.service";

@Injectable()

export class HttpService {
  private APP_HOST;
  public options = new RequestOptions({});
  public headersUrlencode = new Headers({'Content-Type': 'application/x-www-form-urlencoded'});

  constructor(
    public http: Http,
    public tools: ToolsService
  ) {
    this.options.headers = new Headers();
    this.options.headers.set("X-SNS-ClientType", CLIENT_TYPE);
    this.APP_HOST = (environment.ENV_HOST + API_VERSION.V1);
    // console.log(this.APP_HOST)
  }

  public get(url: string, open: boolean = false): Observable<Response> {
    const HOST = open ? (environment.ENV_HOST + API_VERSION.OPEN) : this.APP_HOST;
    const URL = HOST + url;
    return this.http.get(URL, this.options);

  }

  public post(url: string, data: any): Observable<Response> {
    const URL = this.APP_HOST + url;
    const body = this.tools.param(data);
    return this.http.post(URL, body, this.options);
  }

  public postWechat(url: string, data: any): Observable<Response> {
    const HOST = environment.ENV_HOST + API_VERSION.OPEN;
    const URL = HOST + url;
    const body = this.tools.param(data);
    return this.http.post(URL, body, this.options);
  }

  public postJson(url: string, data: any): Observable<Response> {
    const URL = this.APP_HOST + url;
    return this.http.post(URL, data, this.options);
  }

  public postJsonUTF(url: string, data: any): Observable<Response> {
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    const options = new ResponseOptions({headers: headers});
    // let body = this.tools.params(data);
    const URL = this.APP_HOST + url;
    // console.log(body);
    return this.http.post(URL, data, options);
  }

  public postUrlencode(url: string, data: any): Observable<Response> {
    const URL = this.APP_HOST + url;
    const body = this.tools.param(data);
    return this.http.post(URL, body, {headers: this.headersUrlencode});
  }

  public postUrlencodeStr(url: string, data: any): Observable<Response> {
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
    const options = new ResponseOptions({headers: headers});
    const body = this.tools.params(data);
    const URL = this.APP_HOST + url;
    // console.log(body);
    return this.http.post(URL, body, options);
  }

  /**
   * 文件上传
   * @param url 接口路径boundary
   * @param file 文件流
   * @author allenou
   */
  public upload(url: string, file: any): Observable<Response> {
    const formData = new FormData();
    const reader = new FileReader();
    const headers = new Headers();

    formData.append(file.name, file);
    reader.readAsText(file, "UTF-8");

    reader.onload = function () {
      headers.append('Content-Type', 'multipart/form-data;boundary=' + this.result);
    };

    const URL = this.APP_HOST + url;
    const options = new ResponseOptions({headers: headers});

    return this.http.post(URL, formData, options);
  }

    uploadImage(url, body) {
      const formData = new FormData();
      const URL = this.APP_HOST + url;
      formData.append('upfile', body);
      return this.http.post(URL, formData);
    }


  public delete(url: string) {
    const URL = this.APP_HOST + url;
    return this.http.delete(URL, {headers: this.headersUrlencode});
  }

  public put(url: string, data: any = '') {
    const URL = this.APP_HOST + url;
    return this.http.put(URL, this.options);
  }

  public putUrlencode(url: string, data: any) {
    const URL = this.APP_HOST + url;
    const body = this.tools.param(data);
    return this.http.put(URL, body, {headers: this.headersUrlencode});
  }

  /**
   * 对复杂JSON对象put方法
   * url:接口url
   * opt:JSON对象
   */
  public putJsonObj(url: string, opt: any) {
    const URL = this.APP_HOST + url;
    const body = JSON.stringify(opt);
    return this.http.put(URL, body, this.options);
  }

  public handleError(error: Response) {
    return Observable.throw('Server Error');
  }


  /**
   * 文件上传
   * @param url 接口路径
   * @param file 文件流
   * @author allenou
   */
  public uploadFile(url: string, file: any, formData: FormData): Observable<Response> {

    const reader = new FileReader();
    const headers = new Headers();
    formData.append(file.name, file);
    reader.readAsText(file, "UTF-8");

    reader.onload = function () {
      headers.append('Content-Type', 'multipart/form-data;boundary=' + this.result);
    };

    const URL = this.APP_HOST + url;
    // let URL = 'http://192.168.31.28:8080/api/v1' + url;
    const options = new ResponseOptions({headers: headers});

    return this.http.post(URL, formData, options);
  }


  public getUrlencode(url: string): Observable<Response> {
    const headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    const options = new ResponseOptions({headers: headers});
    const URL = this.APP_HOST + url;

    return this.http.get(URL, options);
  }
}
